iOS development Uitextview implement first line indent and default text examples

Source: Internet
Author: User


You may encounter a variety of effects when using Uitextview, for example, in front of the first line of Uitextview the hint text, and after the text entered in the hint after the word, in the input before the default text, the input process the default text will disappear hidden, the specific implementation code as follows:

#import "UItextviewSuoJinViewController.h"

@interface Uitextviewsuojinviewcontroller () <uitextviewdelegate>{
Opinion content
Uitextview *contenttextview;
Cover a uilable on top of the Uitextview.
Uilabel *promptlabel;
}

@end

@implementation Uitextviewsuojinviewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Graycolor];
Opinion content
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 = @ "* Your opinion";
[Contenttextview Addsubview:contentlabel];

Cover a uilable on top of the Uitextview.
Promptlabel = [[Uilabel alloc] init];
Promptlabel.frame =cgrectmake (5,5,200,25);
Promptlabel.text = @ "Please input comments";
promptlabel.enabled = NO;
Promptlabel.backgroundcolor = [Uicolor Clearcolor];
Promptlabel.font = [Uifont systemfontofsize:13];
Promptlabel.textcolor = RGB (245, 245, 245);
[Contenttextview Addsubview:promptlabel];

Change the color of the pentagram
nsmutableattributedstring *contentstr = [[Nsmutableattributedstring alloc] initWithString:contentLabel.text];
[Contentstr addattribute:nsforegroundcolorattributename Value:[uicolor Redcolor] Range:NSMakeRange (0, 1)];
Contentlabel.attributedtext = Contentstr;
}
Proxy method of #pragma mark-uitextview
-(void) Textviewdidchange: (Uitextview *) textview{
if (TextView.text.length = = 0) {
Promptlabel.hidden = NO;
}else{
Promptlabel.hidden = YES;
}
First Line Indent
Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init];
paragraphstyle.linespacing = 3; Line spacing
Paragraphstyle.maximumlineheight = 60; /** Maximum line Height * *
Paragraphstyle.firstlineheadindent = 93.F; /** First Line Indent width * *
Paragraphstyle.alignment = nstextalignmentjustified;
Nsdictionary *attributes = @{
Nsfontattributename:[uifont Systemfontofsize:13],
Nsparagraphstyleattributename:paragraphstyle
};
Textview.attributedtext = [[Nsattributedstring alloc] InitWithString:textView.text attributes:attributes];
}

You can copy the code to your own project file to see the effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.