UITextView 詳解


UITextView 邊框的設置   設置光標的位置
 
導入QuartzCote框架:

#import <QuartzCore/QuartzCore.h>

 

textView.layer.borderColor = [UIColor grayColor].CGColor;

textView.layer.borderWidth =1.0;

textView.layer.cornerRadius =5.0;

 

建立一個UITextView,默認啟動鍵盤,並將光標定位到首位置,因為UITextFiled類沒有此功能,所以改用UItextView.
代碼如下:
Cpp代碼 
UITextView *m_contentTextField = [[[UITextView alloc] init] autorelease];  
m_contentTextField = [[[UITextView alloc] init] autorelease];  
m_contentTextField.frame = CGRectMake(0, 0, 320, 90) ;  
m_contentTextField.backgroundColor = [UIColor whiteColor] ;  
m_contentTextField.font = [UIFont systemFontOfSize:14];  
m_contentTextField.delegate = self ;  
設置此UITextView為第一響應者,即默認打開鍵盤。
Cpp代碼 
[m_contentTextField becomeFirstResponder];  
當UITextView中含有文字時,系統默認將光標定位到最后的位置,下面的語句將光標定位到首位置。
Cpp代碼 
m_contentTextField.selectedRange = NSMakeRange(0,0);  

參考文獻:https://discussions.apple.com/message/12209784#12209784

 

iOS:個性化UITextView(縮進,行距,鋪滿)(點擊可進,已試用,可行)

總體來說個性化定制UITextView中的內容有兩種方法:
1,從文件中讀取內容到UITextView,這個個人感覺使用rtfd和rtf格式文件效果非常好。

2,使用NSAttributeString進行定制

具體方法如下: 

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];  
    paragraphStyle.lineHeightMultiple = 20.f;  
    paragraphStyle.maximumLineHeight = 25.f;  
    paragraphStyle.minimumLineHeight = 15.f;  
    paragraphStyle.firstLineHeadIndent = 20.f;  
paragraphStyle.alignment = NSTextAlignmentJustified;  
  
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]  
                                 };  
 textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];  
 

當然也可以初始化一個NSMutableAttributedString,然后向里面添加文字樣式,最后將它賦給textView的AttributedText即可 

NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];  
    [atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];  
    textView.attributedText = atr;  
 

另外,對於textview中的鏈接樣式,同樣也可以定制 

NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],  
                                     NSUnderlineColorAttributeName: [UIColor blackColor],  
                                     NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};  
self.linkTextAttributes = linkAttributes;  
 

這里只是個簡單的例子,具體還有很多屬性可以自行參考頭文件

 

UITextView 文本換行

從XML或者json中讀取出來的"\n",系統認為是字符串,會默認轉換為"\\n",所以當顯示的時候就是字符串了,要想顯示換行,需要自己手動將"\\n"轉換為"\n",這樣才能換行.

NSString*b =[a stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

 

設置顯示內容的padding

textView.textContainerInset = UIEdgeInsetsMake(0, 10, 0, 10);

效果是右側的滾動條距離內容10像素

參考:How to set margins (padding) in UITextView?

 

 

 


免責聲明!

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



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