uilabel 和uitextview 自適應大小

 

本文轉載至 http://blog.csdn.net/liulichao20/article/details/8957752

分類: ios   321人閱讀  評論(0)  收藏  舉報

 //UILabel自動換行,自適應高度html

    UILabel *label = [[UILabel allocinitWithFrame:CGRectZero];ios

    [label setBackgroundColor:[UIColor clearColor]];app

    [label setFont:[UIFont systemFontOfSize:14]];post

    [label setLineBreakMode:UILineBreakModeWordWrap];spa

    [label setNumberOfLines:0];.net

    [self.view addSubview:label];code

    

    NSString *str = @" fsdfadsfdsfsdfads  fdsfads水電費 水電費水電費水電費水電費水電費水電費水電費水電費水電費 水電費水電費水電費 水電費時代fsdf ";htm

    CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:14]constrainedToSize:CGSizeMake(320,500lineBreakMode:UILineBreakModeWordWrap];blog

    [label setText:str];ip

    [label setFrame:CGRectMake(0.0f20.0f300, size.height)];

    [label release];

    

    //UITextView自動換行,自適應高度

    UITextView *textView = [[UITextView allocinitWithFrame:CGRectMake(0150320,240)];

    textView.backgroundColor = [UIColor clearColor];

    textView.text = str;

    textView.scrollEnabled = YES;

    textView.font = [UIFont systemFontOfSize:14];

    textView.userInteractionEnabled = NO;

    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    [self.view addSubview:textView];

 

本文轉載至  http://tangchuanyao.com/20120507760/

自動調整UITextView/UILabel的高度height

不少時候都須要依據用戶輸入的內容自動調整UILabel/UITextView的高度和寬度,特別是UINavigationController的標題,超過一行的時候默認就是「…」咱們但願他能換行表示,這樣就須要根據內容調整titleView的高度啦。直接貼sample代碼,高度和寬度能夠根據本身的須要調整。

UILabel Sample code

?
1
2
3
4
5
6
7
CGRect frame = CGRectMake(20, 0, 280,44);
CGSize labelsize = [titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize: 16.0f]
                    constrainedToSize:CGSizeMake(320, 44)
                        lineBreakMode:UILineBreakModeTailTruncation];
frame.size.width = labelsize.width;
frame.size.height = labelsize.height;
titleLabel.frame = frame;

UITextView Sample code

?
1
2
3
4
5
6
CGRect frame = noteTextView.frame;
CGSize size = [noteTextView.text sizeWithFont:noteTextView.font
                         constrainedToSize:CGSizeMake(280, 1000)
                             lineBreakMode:UILineBreakModeTailTruncation];
frame.size.height = size.height > 1 ? size.height + 20 : 64;
noteTextView.frame = frame;

UITextView是UIScrollView的子類,所以有contentSize屬性,也能夠按以下實現

?
1
2
3
CGRect frame = noteTextView.frame;
frame.size.height = noteTextView.contentSize.height;
noteTextView.frame = frame;

 

本文轉載至   http://blog.sina.com.cn/s/blog_759d3e120101alji.html

蘋果API

UILineBreakMode

Options for wrapping and truncating text. (Deprecated. Use NSLineBreakMode instead.)

typedef enum { 
   UILineBreakModeWordWrap = 0, 
   UILineBreakModeCharacterWrap, 
   UILineBreakModeClip, 
   UILineBreakModeHeadTruncation, 
   UILineBreakModeTailTruncation, 
   UILineBreakModeMiddleTruncation, 
} UILineBreakMode;

NSLineBreakMode

These constants specify what happens when a line is too long for its container.

enum {
   NSLineBreakByWordWrapping = 0,
   NSLineBreakByCharWrapping,
   NSLineBreakByClipping,
   NSLineBreakByTruncatingHead,
   NSLineBreakByTruncatingTail,
   NSLineBreakByTruncatingMiddle
};
typedef NSUInteger NSLineBreakMode


 

lineBreak模式在6.0以前一直用UILineBreakMode枚舉類型,6.0使用NSLineBreakMode枚舉類型。枚舉值中各個值的意義,解釋以下:

   UILineBreakModeWordWrap = 0, 
   以單詞爲單位換行,以單位爲單位截斷。 
   UILineBreakModeCharacterWrap, 
   以字符爲單位換行,以字符爲單位截斷。 
   UILineBreakModeClip, 
   以單詞爲單位換行。以字符爲單位截斷。 
   UILineBreakModeHeadTruncation, 
   以單詞爲單位換行。若是是單行,則開始部分有省略號。若是是多行,則中間有省略號,省略號後面有4個字符。 
   UILineBreakModeTailTruncation, 
   以單詞爲單位換行。不管是單行仍是多行,都是末尾有省略號。 
   UILineBreakModeMiddleTruncation, 
   以單詞爲單位換行。不管是單行仍是多行,都是中間有省略號,省略號後面只有2個字符。      
相關文章
相關標籤/搜索