ios開發之UITextView實現首行文字縮排和預設文字例子

來源:互聯網
上載者:User


在使用UITextView時可能會遇到各種各樣的效果,例如在UITextView首行前面有提示文字,並且後面輸入的內容在提示的文字後面,在輸入之前有預設的文字,輸入過程中預設的文字會消失隱藏,具體實現代碼如下:

#import "UItextviewSuoJinViewController.h"
 
@interface UItextviewSuoJinViewController ()<UITextViewDelegate>{
    //意見內容
    UITextView *contentTextView;
    //在UITextView上面覆蓋個UILable
    UILabel *promptLabel;
}
 
@end
 
@implementation UItextviewSuoJinViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    //意見內容
    contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, 86)];
    contentTextView.font = FONT(13);
    contentTextView.delegate = self;
    [self.view addSubview:contentTextView];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, 60, 35)];
    contentLabel.font = FONT(13);
    contentLabel.text = @"*您的意見";
    [contentTextView addSubview:contentLabel];
    
    //在UITextView上面覆蓋個UILable
    promptLabel = [[UILabel alloc] init];
    promptLabel.frame =CGRectMake(5,5,200,25);
    promptLabel.text = @"                         請輸入意見";
    promptLabel.enabled = NO;
    promptLabel.backgroundColor = [UIColor clearColor];
    promptLabel.font =  [UIFont systemFontOfSize:13];
    promptLabel.textColor = RGB(245, 245, 245);
    [contentTextView addSubview:promptLabel];
    
    //改變五角星的顏色
    NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
    contentLabel.attributedText = contentStr;
}
#pragma mark -UITextView的代理方法
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        promptLabel.hidden = NO;
    }else{
        promptLabel.hidden = YES;
    }
    //首行縮排
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;    //行間距
    //    paragraphStyle.maximumLineHeight = 60;   /**最大行高*/
    paragraphStyle.firstLineHeadIndent = 93.f;    /**首行縮排寬度*/
    paragraphStyle.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:13],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}

可以將代碼拷到自己的工程檔案中實現看看效果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.