UIButton textLabel有不同的字体

Geo*_*sda 3 uibutton ios

有没有办法在textLabel中使用两种不同字体大小的UIButton?原生?

我不想让UILabel在上面.

Gor*_*ium 14

您可以在界面构建器中执行此操作.此GIF将向您展示如何增加文本的一个部分的大小,并可能更改其字体.

在此输入图像描述

要在代码中执行此操作:

NSString *fullString = @"This bit's plain. This bit's bigger";
NSRange rangeOfPlainBit = [fullString rangeOfString:@"This bit's plain."];
NSRange rangeOfBigBit = [fullString rangeOfString:@"This bit's bigger"];

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:fullString];
[attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"My-font" size:15.0],
                                NSForegroundColorAttributeName: [UIColor whiteColor]}
                        range:rangeOfPlainBit];
[attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"My-font" size:25.0],
                                NSForegroundColorAttributeName: [UIColor whiteColor]}
                        range:rangeOfBigBit];

[self.myButton setAttributedTitle:attributedText forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)


Svi*_*ana 5

SWIFT 3

 func customizeButtonFont(fullText: String, mainText: String, creditsText: String, button: UIButton) {
        let fontBig = UIFont(name:"SFUIDisplay-Medium", size: 16.0)
        let fontSmall = UIFont(name:"SFUIDisplay-Light", size: 14.0)
        let attributedString = NSMutableAttributedString(string: fullText, attributes: nil)

        let bigRange = (attributedString.string as NSString).range(of: mainText)
        let creditsRange = (attributedString.string as NSString).range(of: creditsText)
        attributedString.setAttributes([NSAttributedStringKey.font: fontBig, NSAttributedStringKey.foregroundColor: UIColor.white], range: bigRange)
        attributedString.setAttributes([NSAttributedStringKey.font: fontSmall, NSAttributedStringKey.foregroundColor: UIColor.white], range: creditsRange)

        button.setAttributedTitle(attributedString, for: .normal)
    }
Run Code Online (Sandbox Code Playgroud)