UIButton titleLabel frame size returning CGSize with zero width & height

前端 未结 6 446
执笔经年
执笔经年 2021-02-01 23:16

For iOS7, I used the UIButton titleLabel.frame.size.width property to determine the width of my button title under different localisations so I could p

6条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 23:43

    Maybe this will not work for you, but I had the same problem when running my code in XCode 6 GM / iOS8 and could not get it to work. It worked randomly and I ended up using a different approach instead of an NSString I used an NSAttributedString as the title.

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"Button text"];
    NSTextAttachment* textAttachment = [NSTextAttachment new];
    textAttachment.image = [UIImage imageNamed:@"Arrows"];
    NSAttributedString *finalString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedText appendAttributedString:finalString];
    

    Only caveat is that it’s iOS7 and up, but seems to work flawless.

提交回复
热议问题