iOS NSAttributedString on UIButton

前端 未结 3 492
谎友^
谎友^ 2020-12-04 13:14

I\'m using iOS 6, so attributed strings should be easy to use, right? Well... not so much.

What I want to do:

Using a custom subclass of

3条回答
  •  一生所求
    2020-12-04 13:48

    It looks to me like you forgot in your code to use the "style" object that you set up.. you just instantiated it. You should modify your code to look like this:

    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [style setAlignment:NSTextAlignmentCenter];
    [style setLineBreakMode:NSLineBreakByWordWrapping];
    
    UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0f];
    UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light"  size:20.0f];
    NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                            NSFontAttributeName:font1,
                            NSParagraphStyleAttributeName:style}; // Added line
    NSDictionary *dict2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                            NSFontAttributeName:font2,
                            NSParagraphStyleAttributeName:style}; // Added line
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"LINE 1\n"    attributes:dict1]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"line 2"      attributes:dict2]];
    [self.resolveButton setAttributedTitle:attString forState:UIControlStateNormal];
    [[self.resolveButton titleLabel] setNumberOfLines:0];
    [[self.resolveButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
    

    Note that I only added the lines that define the NSParagraphStyleAttributeName.. everything else is the same.. and this is what I get for the button:

    enter image description here

    And here it is in Swift 3.0

    let style = NSMutableParagraphStyle()
    style.alignment = .center
    style.lineBreakMode = .byWordWrapping
    
    guard
        let font1 = UIFont(name: "HelveticaNeue-Medium", size: 20),
        let font2 = UIFont(name: "HelveticaNeue-Light", size: 20)  else { return }
    
    let dict1:[String:Any] = [
        NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
        NSFontAttributeName:font1,
        NSParagraphStyleAttributeName:style
    ]
    
    let dict2:[String:Any] = [
        NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
        NSFontAttributeName:font2,
        NSParagraphStyleAttributeName:style
    ]
    
    let attString = NSMutableAttributedString()
    attString.append(NSAttributedString(string: "LINE 1", attributes: dict1))
    attString.append(NSAttributedString(string: "line 2", attributes: dict2))
    
    button.setAttributedTitle(attString, for: .normal)
    button.titleLabel?.numberOfLines = 0
    button.titleLabel?.lineBreakMode = .byWordWrapping
    

提交回复
热议问题