IOS Uitextview Adaptive height based on input text

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/tmf-4838/p/5380495.html

#import "ViewController.h"

@interface Viewcontroller () <UITextViewDelegate>

KVO and Dynamic Adaptive dimensions

@property (nonatomic, strong) Uitextview *txtview;

KVO Test

@property (nonatomic, strong) person *person;

@end

@implementation Viewcontroller

-(void) viewdidload

{

[Super Viewdidload];

1. Looping when the number of words is greater than the label length

Create a third-party control LBL Object

Bbflashctntlabel *BFLBL = [[Bbflashctntlabel alloc] initwithframe:cgrectmake (107, 100, 200, 50)];

Replace background color

Bflbl.backgroundcolor = [Uicolor Purplecolor];

Property string

nsmutableattributedstring *attstr = [[Nsmutableattributedstring alloc] initwithstring:@ "January 2016, With the release of the 9.2.1 release, Apple has fixed a 3-year-old bug. The vulnerability when an iphone or ipad user accesses a network with a mandatory portal, such as a hotel or an airport, the login page displays the network terms of use via an unencrypted HTTP connection. After the user accepts the terms, the Internet is normal, but the embedded browser will share the unencrypted cookie with the Safari browser. Using this shared resource, a hacker can create an autonomous, bogus mandatory portal and link it to a WiFi network to steal any unencrypted cookie stored on the device "];

Font

[Attstr addattribute:nsfontattributename Value:[uifont systemfontofsize:25] Range:nsmakerange (0, attStr.length)];

Font background color

[Attstr addattribute:nsbackgroundcolorattributename Value:[uicolor Bluecolor] Range:nsmakerange (0, attStr.length)];

Font foreground color

[Attstr addattribute:nsforegroundcolorattributename Value:[uicolor Whitecolor] Range:nsmakerange (0, AttStr.length)] ;

Round Corners

BFLbl.layer.cornerRadius = 15;

To add a property string to a label

Bflbl.attributedtext = Attstr;

Limit playback speed to a label

Bflbl.speed = Bbflashctntspeedmild;

Cyclic scrolling (for 0 o'clock infinite scrolling)

Bflbl.repeatcount = 0;

[Self.view ADDSUBVIEW:BFLBL];

2. Custom dimensions when the word count is greater than the label length

Create a Label object

UILabel *LBL = [[UILabel alloc] init];

Adaptive size so the number of rows is 0

Lbl.numberoflines = 0;

Set Background color

Lbl.backgroundcolor = [Uicolor Redcolor];

Text content

Lbl.text = @ "January 2016, with the release of the 9.2.1 release, Apple fixed a 3-year-old vulnerability. The vulnerability when an iphone or ipad user accesses a network with a mandatory portal, such as a hotel or an airport, the login page displays the network terms of use via an unencrypted HTTP connection. After the user accepts the terms, the Internet is normal, but the embedded browser will share the unencrypted cookie with the Safari browser. Using this shared resource, a hacker can create an autonomous, bogus mandatory portal and link it to a WiFi network to steal any unencrypted cookies stored on the device;

Set Text color

Lbl.textcolor = [Uicolor Whitecolor];

Set up adaptive paragraphs

Nsmutableparagraphstyle *para = [Nsmutableparagraphstyle new];

Para.linebreakmode = nslinebreakbywordwrapping;

Create a dictionary carrying properties: Font size depends on fonts

Nsdictionary *dic = @{nsfontattributename:lbl.font, Nsparagraphstyleattributename:para};

Get the font size: LbL's font is calculated

CGRect rect = [Lbl.text boundingrectwithsize:cgsizemake (Cgrectgetheight (self.view.frame)) options: Nsstringdrawinguseslinefragmentorigin Attributes:dic Context:nil];

Just the size of the label (the height of the negative can be achieved by moving upwards)

Lbl.frame = CGRectMake (HEI-10, Rect.size.width,-rect.size.height);

[Self.view ADDSUBVIEW:LBL];

Dynamic acquisition of dimensions

_txtview = [[Uitextview alloc] init];

_txtview.backgroundcolor = [Uicolor Redcolor];

_txtview.delegate = self;

_txtview.text = @ "January 2016, with the release of the 9.2.1 release, Apple fixed a 3-year-old vulnerability. The vulnerability when an iphone or ipad user accesses a network with a mandatory portal, such as a hotel or an airport, the login page displays the network terms of use via an unencrypted HTTP connection. ";

[Self textviewdidchange:_txtview];

NSLog (@ "%@", self.txtView.font.fontDescriptor);

NSLog (@ "%@", self.txtView.font.description);

[Self.view AddSubview:self.txtView];

KVO

Used in the UI to Kvo: General system controls have event snooping, most of which are custom classes \

You need to create at least one property (that is, a global variable), and if you create a local variable, the listener will be destroyed once again, and the program will crash.

Because the listener is assigned a value by overriding its setter to add the listener, there is no reference counter plus 1, so at least one of the global variables is required

Overriding the setter only implements the listener: so the listener object needs to be assigned to the listener, triggering the addition of the Listener event listener start

Self.person = [[Person alloc] init];

Add Listener

TXT *TT = [txt new];

Tt.per = Self.person;;

Self.person.name = @ "PP";

Can only listen to the assignment, input on the screen can not be heard

Tt.txtview = Self.txtview;

Self.txtView.text = @ "11";

}

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

{

Text gets the characters that are entered each time, Range.location is the character position

return YES;

}

Agent listener: This method is called when text changes

-(void) Textviewdidchange: (Uitextview *) TextView

{

Nsmutableparagraphstyle *para = [Nsmutableparagraphstyle new];

Para.linebreakmode = nslinebreakbywordwrapping;

Create a dictionary carrying properties: Font size depends on fonts

Nsdictionary *dic = @{nsfontattributename:_txtview.font, Nsparagraphstyleattributename:para};

Get the font size: LbL's font is calculated

CGRect rect = [_txtview.text boundingrectwithsize:cgsizemake (0,0) options:nsstringdrawingusesfontleading attributes: DiC Context:nil];

Cgsize size = [_txtview sizethatfits:cgsizemake (Rect.size.height)];

_txtview.frame = CGRectMake (+, +,-size.height);

#import <Foundation/Foundation.h>

@class Uitextview, person;

@interface Txt:nsobject

Overriding the setter only implements the listener: so the listener object needs to be assigned to the listener, triggering the addition of the Listener event listener start

@property (nonatomic, strong) Uitextview *txtview;

@property (nonatomic, strong) person *per;

#import "Txt.h"

#import "Person.h"

#import <UIKit/UIKit.h>

@implementation TXT

Used in the UI to Kvo: General system controls have event snooping, most of which are custom classes \

You need to create at least one property (that is, a global variable), and if you create a local variable, the listener will be destroyed once again, and the program will crash.

Because the fly listener assignment is to add listening by overriding its setter, no reference counter plus 1, so at least one of the global variables is required

Add Listener

Overriding the setter only implements the listener: so the listener object needs to be assigned to the listener, triggering the addition of the Listener event listener start

-(void) Setper: (person *) per

{

_per = per;

[Self.per addobserver:self forkeypath:@ "name" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];

}

Add Listener

-(void) Settxtview: (Uitextview *) Txtview

{

_txtview = Txtview;

[Self.txtview addobserver:self forkeypath:@ "text" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];

}

Register for monitoring

-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary<nsstring *,id> *) Change context: (void *) context

{

if ([KeyPath isequaltostring:@ "name"])

{

NSLog (@ "pp old value.%@", [change Objectforkey:nskeyvaluechangeoldkey]);

NSLog (@ "pp new value.%@", [change Objectforkey:nskeyvaluechangenewkey]);

}else if ([KeyPath isequaltostring:@ "text"])

{

NSLog (@ "txt old value.%@", [change Objectforkey:nskeyvaluechangeoldkey]);

NSLog (@ "txt new value.%@", [change Objectforkey:nskeyvaluechangenewkey]);

}

}

Remove listener: Sets the pointer to null

-(void) Delete: (ID) sender

{

[Self.per removeobserver:self forkeypath:@ "name" Context:nil];

Self.per = nil;

[Self.txtview removeobserver:self forkeypath:@ "text"];

Self.txtview = nil;

}

#import <Foundation/Foundation.h>

@interface Person:nsobject

@property (nonatomic, strong) NSString *name;

@end

#import "Person.h"

@implementation person

/*

-(Instancetype) init

{

if (self = [super init])

{

_name = @ "* * *";

}

return self;

}

*/

-(NSString *) description

{

return [NSString stringwithformat:@ "%@", _name];

}

@end

}

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

{

[Self.view Endediting:yes];

}

IOS Uitextview Adaptive height based on input text

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.