iOS development Uitextview, set TextView line spacing and placeholder

Source: Internet
Author: User

First, set the line spacing of the TextView

1. If you just statically display the TextView content as the set line spacing, execute the following code:

//???? TextView Change the line spacing of the font?
???? Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init];?
???? paragraphstyle.linespacing = line spacing for 10;//fonts?
?????
???? Nsdictionary *attributes = @{?
???????????????????????????????? Nsfontattributename:[uifont systemfontofsize:15],?
???????????????????????????????? Nsparagraphstyleattributename:paragraphstyle?
???????????????????????????????? };?
???? Textview.attributedtext = [[Nsattributedstring alloc] initwithstring:@ "Enter your Content" attributes:attributes];

?

2. If you want to dynamically change the line spacing as you enter it, you need to put the above code in the TextView delegate method

-(void) Textviewdidchange: (Uitextview *) TextView

{

? ? //? ? TextView changing the line spacing of a font

? ? Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init];

? ? paragraphstyle.linespacing = line spacing for 20;//fonts

?? ?

? ? Nsdictionary *attributes = @{

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Nsfontattributename:[uifont Systemfontofsize:15],

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Nsparagraphstyleattributename:paragraphstyle

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

? ? Textview.attributedtext = [[Nsattributedstring alloc] InitWithString:textView.text attributes:attributes];

?

}

?

First, set the placeholder of TextView

Uitextview on how to add similar to Uitextfield placeholder, in fact, add a Uilabel or Uitextview on Uitextview, if used uilable, A problem arises when placeholder's text is too long to break the line, and using Uitextview can effectively avoid this problem.

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text

{?? if (![ Text isequaltostring:@ ""])

? ? ? ? {

? ? ? ? ? ? _placeholderlabel.hidden = YES;

? ? ? ? }

?? ? if ([Text isequaltostring:@ ""] && range.location = = 0 && Range.length = = 1)

? ? ? ? {

? ? ? ? ? ? _placeholderlabel.hidden = NO;

? ? ? ? }

? ? return YES;

}

?

The description is as follows:

? (1) _placeholderlabel? is added to the uitextview behind the Uitextview,_placeholderlabel to ensure that the same as the real input box settings, the font is set to light gray, and then [_ Placeholderlabel Seteditable:no]; True input box to set the background color transparent, guaranteed to see the bottom of the _placeholderlabel.

? ? (2) [Text isequaltostring:@ ""] means the BACKSPACE key is entered

? ? (3) Range.location = = 0 && Range.length = = 1 indicates the first character entered

iOS development Uitextview, set TextView line spacing and placeholder

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.