uilabel 和uitextview 自適應大小


 

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

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

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

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

    [label setBackgroundColor:[UIColor clearColor]];

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

    [label setLineBreakMode:UILineBreakModeWordWrap];

    [label setNumberOfLines:0];

    [self.view addSubview:label];

    

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

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

    [label setText:str];

    [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個字符。      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM