how to avoid dots next to a uitextfield

邮差的信 提交于 2019-12-08 03:51:19

问题


Is that possible to remove the dots which are displayed next to uitextfield when text is larger than the width of the uitextfield.


回答1:


There is a property lineBreakMode which defines the behaviour when the text to be displayed is larger than the box of the control

//UILineBreakMode
//Options for wrapping and truncating text.

typedef enum {
 UILineBreakModeWordWrap = 0,
 UILineBreakModeCharacterWrap,
 UILineBreakModeClip,
 UILineBreakModeHeadTruncation,
 UILineBreakModeTailTruncation,
 UILineBreakModeMiddleTruncation,
} UILineBreakMode;

If you do not want any dots you can use UILineBreakModeClip which simply cuts the text off at the end. The apple doc says this:

Clip the text when the end of the drawing rectangle is reached. This option could result in a partially rendered character at the end of a string. Available in iPhone OS 2.0 and later.

Although it of course depends on how you want to display the text. Since either way you cannot display all of the text the dots are usually more asthetic than a raw break.

If you require absolutely all of the text to be visible you must either expand the width of the field, or the height (and use UILineBreakModeCharacterWrap), or possibly decrease the font size (as a last resort).

A quick google search on the subject pulled this How to make UILabel / UITableViewCell to have a dynamic height




回答2:


It is possible if you make keyboard visible calling following code

[self.yourTextField becomeFirstResponder];

Then larger text is clipped from left.

you can also hide keyboard [if you use any custom keyboard] using following code

// Hide keyboard for Dial Pad, but show blinking cursor
UIView *dummyKeyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
yourTextField.inputView = dummyKeyboardView;
[dummyKeyboardView release];



回答3:


I tried lots of ways and finally I came to following solution.

UITextField does not have a lineBreakMode property so just override following method in UITextField:

-(CGRect)textRectForBounds:(CGRect)bounds{

    CGRect rect = bounds;
    rect.size.width = rect.size.width;
    return rect;
}

Note: I dont know how, but it works.



来源:https://stackoverflow.com/questions/3287312/how-to-avoid-dots-next-to-a-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!