2013-10-24 50 views

回答

3

這實際上不是關於NSAttributedStringNSString,而是關於按鈕本身。你可能會認爲繼承UIButton和壓倒一切的setTitle:forState:自動替換用\n第一空間。

具體的setTitle:forState:會是這個樣子

- (void)setTitle:(NSString *)title forState:(UIControlState)state { 
    NSRange firstSpaceRanger = [title rangeOfString:@" "]; 
    if (firstSpaceRanger.location != NSNotFound) { 
     title = [title stringByReplacingCharactersInRange:firstSpaceRanger withString:@"\n"]; 
    } 
    [super setTitle:title forState:state]; 
} 

例如,給定one two three這將產生

one 
two three 

爲了使按鈕多,你可以做,在UIButton初始化:

self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 

是的,firstSpaceRanger是故意的。我無法抗拒。

相關問題