我想調整我的UITextView中的文本的大小,使它適合。我有以下代碼:基於UITextView幀大小調整文本
NSString *storyTitle = [newsfeedToSubject.properties valueForKey:@"title"];
int currentFontSize = 18;
UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
}
[self.newsfeedStoryTitle_ setFont:currentFont];
[self.newsfeedStoryTitle_ setText:storyTitle];
[self.newsfeedStoryTitle_ setBackgroundColor:[UIColor redColor]];
然而,這裏就是我得到:
我試着用不同的換行模式,還可以設置自動調整大小爲無,但是事實並非如此幫幫我。任何想法?
yea..the問題是我不希望調整框架..我想要調整的文本 – adit 2012-07-31 18:16:21
如何計算換行符的數量? – adit 2012-07-31 18:18:40
請參閱NSString UIKit Additions Reference(https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html),特別是'sizeWithFont:forWidth:lineBreakMode'方法。這應該會返回一個CGSize,它應該具有適當的字符串高度。我不在我的Mac上進行驗證,但它應該能夠滿足您的需求。也就是說,如果返回的高度大於靜態框架,請減少字體大小,然後重試。 – jwest 2012-07-31 18:39:27