如何使用 NSMutableAttributedString 设置 UILabel不是 UITextView圆角背景?

问题描述

无论如何我都需要在 UITableView 中创建代码样式。

简单制作蓝色背景

var dynamicBackground = #colorLiteral(red: 0.02708261088,green: 0.4768417478,blue: 0.9985074401,alpha: 1)

然后在 UITableView 中创建 NSMutableAttributedString

let attr = NSMutableAttributedString(string: "if ",attributes: [.foregroundColor: KeyboardSyntax])
             
  attr.append(NSAttributedString(string: " false condition ",attributes: [.backgroundColor: dynamicBackground])). // Any idea to make background to round corner?
            
  attr.append(NSAttributedString(string: " {\n ",attributes: [.foregroundColor: PlainSyntax]))
            
  attr.append(NSAttributedString(string: "// Not execute\n",attributes: [.foregroundColor: CommentSyntax]))
            
  attr.append(NSAttributedString(string: "} ",attributes: [.foregroundColor: PlainSyntax]))
            
  attr.append(NSAttributedString(string: "else ",attributes: [.foregroundColor: KeyboardSyntax]))
            
  attr.append(NSAttributedString(string: "{\n ",attributes: [.foregroundColor: PlainSyntax]))
            
  attr.append(NSAttributedString(string: "// Execute\n",attributes: [.foregroundColor: CommentSyntax]))
               
  attr.append(NSAttributedString(string: "} ",attributes: [.foregroundColor: PlainSyntax]))
            
  cell.textLabel?.attributedText = attr

然后显示结果

enter image description here

感谢您的热心帮助。 :)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)