【问题标题】:Rounded corners to UILabel in NSMutableAttributedStringNSMutableAttributedString 中 UILabel 的圆角
【发布时间】:2017-03-23 12:27:03
【问题描述】:

我有一个具有背景颜色和圆角的可变文本。所以我将 UILabel 与 NSMutableAttributedString 一起使用。

    myAttributedString = NSMutableAttributedString(string:"hello")
    myAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: 0, length: myAttributedString.length))
    myLabel.attributedText = myAttributedString
    myLabel.layer.cornerRadius = 5
    myLabel.clipsToBounds = true
    myLabel.layer.masksToBounds = true

我得到一个只有一侧有圆角的标签。我应该怎么做才能得到四面八方的圆角?

【问题讨论】:

  • @karthikeyan 我已经添加了cornerRadius 和 masktoBounds,也尝试添加 sizeToFit ......但我得到了相同的结果,
  • 问题出在其他地方,您是否使用 uibeizerpath 作为标签
  • @Anbu.Karthik nope
  • 没问题不在这一行

标签: ios swift3 uilabel nsmutableattributedstring


【解决方案1】:

只是增加UILabel(myLabel)的宽度

let myAttributedString = NSMutableAttributedString(string:"hello")
        myAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: 0, length: myAttributedString.length))
        myLabel.attributedText = myAttributedString
        myLabel.layer.cornerRadius = 5
        myLabel.clipsToBounds = true
        myLabel.layer.masksToBounds = true

// your code is right
// just increase the width of UILabel (myLabel)

【讨论】:

  • 我无法为标签设置固定宽度,因为内容会随时更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多