Bold & Non-Bold Text In A Single UILabel?

后端 未结 14 2193
别那么骄傲
别那么骄傲 2020-11-22 08:51

How would it be possible to include both bold and non-bold text in a uiLabel?

I\'d rather not use a UIWebView.. I\'ve also read this may be possible using NSAttribut

相关标签:
14条回答
  • 2020-11-22 09:24

    Check out TTTAttributedLabel. It's a drop-in replacement for UILabel that allows you to have mixed font and colors in a single label by setting an NSAttributedString as the text for that label.

    0 讨论(0)
  • 2020-11-22 09:25

    I've adopted Crazy Yoghurt's answer to swift's extensions.

    extension UILabel {
    
        func boldRange(_ range: Range<String.Index>) {
            if let text = self.attributedText {
                let attr = NSMutableAttributedString(attributedString: text)
                let start = text.string.characters.distance(from: text.string.startIndex, to: range.lowerBound)
                let length = text.string.characters.distance(from: range.lowerBound, to: range.upperBound)
                attr.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: self.font.pointSize)], range: NSMakeRange(start, length))
                self.attributedText = attr
            }
        }
    
        func boldSubstring(_ substr: String) {
            if let text = self.attributedText {
                var range = text.string.range(of: substr)
                let attr = NSMutableAttributedString(attributedString: text)
                while range != nil {
                    let start = text.string.characters.distance(from: text.string.startIndex, to: range!.lowerBound)
                    let length = text.string.characters.distance(from: range!.lowerBound, to: range!.upperBound)
                    var nsRange = NSMakeRange(start, length)
                    let font = attr.attribute(NSFontAttributeName, at: start, effectiveRange: &nsRange) as! UIFont
                    if !font.fontDescriptor.symbolicTraits.contains(.traitBold) {
                        break
                    }
                    range = text.string.range(of: substr, options: NSString.CompareOptions.literal, range: range!.upperBound..<text.string.endIndex, locale: nil)
                }
                if let r = range {
                    boldRange(r)
                }
            }
        }
    }
    

    May be there is not good conversion between Range and NSRange, but I didn't found something better.

    0 讨论(0)
  • 2020-11-22 09:26

    Swift 4:

    // attribute with color red and Bold
    var attrs1 = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedStringKey.foregroundColor: UIColor.red]
    
    // attribute with color black and Non Bold
    var attrs2 = [NSAttributedStringKey.font: UIFont(name: "Roboto-Regular", size: 20), NSAttributedStringKey.foregroundColor: UIColor.black]  
    
    var color1 = NSAttributedString(string: "RED", attributes: attrs1)
    
    var color2 = NSAttributedString(string: " BLACK", attributes: attrs2)
    
    var string = NSMutableAttributedString()
    
    string.append(color1)
    
    string.append(color2)
    
    // print the text with **RED** BLACK
    print("Final String : \(string)")
    
    0 讨论(0)
  • 2020-11-22 09:27

    Try a category on UILabel:

    Here's how it's used:

    myLabel.text = @"Updated: 2012/10/14 21:59 PM";
    [myLabel boldSubstring: @"Updated:"];
    [myLabel boldSubstring: @"21:59 PM"];
    

    And here's the category

    UILabel+Boldify.h

    - (void) boldSubstring: (NSString*) substring;
    - (void) boldRange: (NSRange) range;
    

    UILabel+Boldify.m

    - (void) boldRange: (NSRange) range {
        if (![self respondsToSelector:@selector(setAttributedText:)]) {
            return;
        }
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        [attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
    
        self.attributedText = attributedText;    
    }
    
    - (void) boldSubstring: (NSString*) substring {
        NSRange range = [self.text rangeOfString:substring];
        [self boldRange:range];
    }
    

    Note that this will only work in iOS 6 and later. It will simply be ignored in iOS 5 and earlier.

    0 讨论(0)
  • 2020-11-22 09:31

    It worked for me:

    CGFloat boldTextFontSize = 17.0f;
    
    myLabel.text = [NSString stringWithFormat:@"%@ 2012/10/14 %@",@"Updated:",@"21:59 PM"];
    
    NSRange range1 = [myLabel.text rangeOfString:@"Updated:"];
    NSRange range2 = [myLabel.text rangeOfString:@"21:59 PM"];
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:myLabel.text];
    
    [attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:boldTextFontSize]}
                            range:range1];
    [attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:boldTextFontSize]}
                            range:range2];
    
    myLabel.attributedText = attributedText;
    

    For Swift version: See Here

    0 讨论(0)
  • 2020-11-22 09:32

    There's category based on bbrame's category. It works similar, but allows you boldify same UILabel multiple times with cumulative results.

    UILabel+Boldify.h

    @interface UILabel (Boldify)
    - (void) boldSubstring: (NSString*) substring;
    - (void) boldRange: (NSRange) range;
    @end
    

    UILabel+Boldify.m

    @implementation UILabel (Boldify)
    - (void)boldRange:(NSRange)range {
        if (![self respondsToSelector:@selector(setAttributedText:)]) {
            return;
        }
        NSMutableAttributedString *attributedText;
        if (!self.attributedText) {
            attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
        } else {
            attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        }
        [attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
        self.attributedText = attributedText;
    }
    
    - (void)boldSubstring:(NSString*)substring {
        NSRange range = [self.text rangeOfString:substring];
        [self boldRange:range];
    }
    @end
    

    With this corrections you may use it multiple times, eg:

    myLabel.text = @"Updated: 2012/10/14 21:59 PM";
    [myLabel boldSubstring: @"Updated:"];
    [myLabel boldSubstring: @"21:59 PM"];
    

    will result with: "Updated: 2012/10/14 21:59 PM".

    0 讨论(0)
提交回复
热议问题