简体   繁体   English

是否可以使用@IBDesignable在情节提要中显示UILabel.attributedText?

[英]Is it possible to display UILabel.attributedText in a storyboard using @IBDesignable?

All the labels in my app are using fonts with spacing/kerning values set as well as colors and several other things and I want to see what thing look like in the storyboard with all of these attributes applied. 我的应用程序中的所有标签都使用设置了间距/字距值以及颜色和其他几种设置的字体,我想看看应用了所有这些属性的情节提要中的外观。

As UILabel is a UIView I was hoping it might be possible to achieve this by using @IBDesignable. 由于UILabel是UIView,我希望可以通过使用@IBDesignable来实现。 But after some experimentation I can't get anything to happen in the storyboard. 但是经过一些试验,我在情节提要中什么也没发生。

This is the code: 这是代码:

@IBDesignable class CustomLabel: UILabel {
    @IBDesignable override var text: String? {
        didSet {
            guard let text = text else { return }
            let titleAttributes = [NSForegroundColorAttributeName: UIColor.green]
            let titleString = NSMutableAttributedString(string: text, attributes: titleAttributes)
            titleString.addAttribute(NSKernAttributeName, value: 5, range: NSRange(location: 0, length: text.characters.count))
            // Other attributes
            self.attributedText = titleString
        }
    }
}

... ...

@IBOutlet weak var theTitle: CustomLabel!
theTitle.text = "Some stuff"

But in the storyboard the label is displayed without any of the attributed string settings applied. 但是在情节提要中将显示标签,而未应用任何属性字符串设置。

Does anybody know if what I'm attempting is going to be possible, and if so how to get it working? 有人知道我正在尝试的事情是否可能吗?如果可以,如何使它正常工作?

Swift 3 迅捷3

As per Your situation use @IBInspectable to overridden text var of UILabel , This will allow you to pass the text from property inspector (see screen shot) which will be rendered on Storyboard 根据您的情况,使用@IBInspectable 覆盖 UILabel的 文本 变量 ,这将允许您传递来自属性检查器的文本(请参见屏幕快照),该文本将在Storyboard上呈现。

在此处输入图片说明

@IBDesignable class DGlabel: UILabel {

@IBInspectable override var text: String? {
    didSet {
        decorate()
    }
}

@IBInspectable var fontSize: CFloat = 15 {
    didSet {
        decorate()
    }
}


@IBInspectable var fontColor: UIColor = UIColor.red {
    didSet {
        decorate()
    }
}

/*** more inspectable var can be added **/

func decorate() {

    guard let text = text else { return }

    let titleAttributes = [
        NSFontAttributeName : UIFont.systemFont(ofSize: fontSize),
        NSForegroundColorAttributeName : fontColor,
        NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue
        ] as [String : Any]

    let titleString = NSMutableAttributedString(string: text, attributes: titleAttributes)

    // Other attributes
    titleString.addAttribute(NSKernAttributeName, value: 5, range: NSRange(location: 0, length: text.characters.count))

    self.attributedText = titleString
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在UILabel.attributedText中建立链接*不是*蓝色,*不是*带下划线 - Make link in UILabel.attributedText *not* blue and *not* underlined 随时随地在UILabel.attributedText上应用不同的属性 - Apply different attributes on UILabel.attributedText on the go 将UILabel.attributedText设置为相同的值是否昂贵? - Expensive to set UILabel.attributedText to the same value? UILabel.attributedText设置属性一次,然后只更改文本(字符串) - UILabel.attributedText set attributes once and then just change text (string) UILabel.attributedText与Emoji字体结合使用时的奇怪行为 - Strange behaviour of UILabel.attributedText combined with Emoji font UILabel.attributedText无法在iPhone 4 + iOS 7.0.3上显示 - UILabel.attributedText not show up on iPhone 4 + iOS 7.0.3 如果我同时设置UILabel.text和UILabel.attributedText会发生什么? - What happens if I set both UILabel.text and UILabel.attributedText? UILabel.attributedText在iPhone Simulator上不显示(但在iPad Simulator上可用) - UILabel.attributedText doesn't show on iPhone Simulator (but works on iPad Simulator) 使用IBDesignable和prepareForInterfaceBuilder与UILabel - Using IBDesignable and prepareForInterfaceBuilder with a UILabel 具有NSMutableAttributedString属性的UICollectionViewCell UILabel缓慢更新和显示 - UICollectionViewCell UILabel with NSMutableAttributedString attributedText slow to update & display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM