相关疑难解决方法(0)

如何调整标签大小以适合文本的长度

我在过去的QAs中搜索了解决方案,但找不到合适的QA.
有谁知道如何UILabel动态调整大小以适应文本长度?
我上传了我不想要的屏幕截图(第1行)和我想要的(第2行).
我很感激任何线索,建议或代码示例.谢谢.在此输入图像描述

objective-c uilabel ios

14
推荐指数
2
解决办法
4万
查看次数

垂直对齐UILabel

我试图在我的应用程序的UILabel视图中垂直对齐文本.问题是我希望文本垂直对齐顶部,标签的大小为280 x 150.我只能实现这两件事之一.如果我删除该行

[myLabel sizeToFit];
Run Code Online (Sandbox Code Playgroud)

然后文本的对齐没问题但是大小搞砸了.但是如果我添加上面的行,那么对齐就搞砸了但是大小没问题.我该如何解决这个问题.

我已添加以下代码 -

CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
Run Code Online (Sandbox Code Playgroud)

xcode uilabel ios

10
推荐指数
4
解决办法
4万
查看次数

UILabel和numberOfLines和sizeToFit:

在iOS 5上.

我有一个最初放在笔尖中的UILabel元素.我希望x位置保持不变.我希望它采用1行或2行.如果超过两行,则应使用换行符设置来显示省略号.

我用numberOfLines属性和-sizeToFit

如果我将UILabel的numberOfLines属性设置为0,它将正确地看到对于某些文本,没有足够的空间并且在调用之后将其包装到第二行-sizeToFit但是在极少数情况下该行足够长以拉伸到3行,我得到三行,我不想要.如果我将numberOfLines属性设置为2,它实际上将整个东西拉伸到一条线上,并将我在笔尖中设置的初始框架拉长得更宽.

 CGRect titleFrame = [[self titleLabel] frame];
 [[self titleLabel] setNumberOfLines:0];
 [[self titleLabel] setText:newProductTitleText];
 [[self titleLabel] sizeToFit];
 CGRect newTitleFrame = [[self titleLabel] frame];
Run Code Online (Sandbox Code Playgroud)

CGRect就在那里,我可以在事后计算出事物.因此设置numberOfLines为0有效,并且不会更改框架中的原始origin.x,并且会将长文本分成多行,但不会将其约束为2行.将numberOfLines属性设置为2,当我阅读Apple文档时

此属性控制使用的最大行数,以使标签的文本适合其边界矩形.此属性的默认值为1.要删除任何最大限​​制,并根据需要使用尽可能多的行,请将此属性的值设置为0.

似乎我应该能够将其设置为两个并仍然有效.我希望sizeToFit在扩展以适应所有文本时在正X和Y方向上扩展,但是当numberOfLines设置为0以外时它在负X方向上扩展.

ETA:"自动调整大小"的struts设置为上部和左侧以将其固定在最小x,y.

感谢您的任何见解.

uilabel text-size sizetofit ios5

10
推荐指数
1
解决办法
2万
查看次数

Swift:将UILabel文本对齐在顶部而不是中间

我希望UILabel从顶部开始,即使文本很短,似乎 NSTextAlignment不起作用

在此输入图像描述

cell.textContent.text = comments[indexPath.row]
cell.textContent.textAlignment = 




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //post's section == 0

    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
        cell.usernameLabel.text = "Steve Paul Jobs"
         cell.time.text = "9:42 PM"
         cell.commentsLabelCount.text = "12 Comments"
         cell.textContent.text = "Return the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn …
Run Code Online (Sandbox Code Playgroud)

text-alignment uilabel ios swift

8
推荐指数
4
解决办法
3万
查看次数

标签文本对齐多行

我有一个带有多行的标签.我希望标签上的文字始终从左上角开始,与标签的高度和行数无关.

现在我正在使用房产

[question1Label setContentMode: UIViewContentModeTopLeft];
Run Code Online (Sandbox Code Playgroud)

但它不起作用

谢谢

iphone label objective-c alignment ios

5
推荐指数
1
解决办法
2万
查看次数

垂直对齐UILabel中的属性文本

设置UILabel的属性文本并使用setFontSizeToFitWidth时,属性文本字体大小按预期调整大小.但是,当属性字符串字体大小调整为较小的字体大小时,文本在UILabel内部不会垂直对齐.

我需要使用adjustFontSizeToFitWidth方法,因为属性字符串具有可变大小.我的最小字体大小设置为"15.0",最大字体大小设置为"28.0".因此我使用minimumScaleFactor"15.0/28.0"

我的代码:

NSAttributedString *balance = [[NSAttributedString alloc] initWithString:@"12390765298374652938756" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

// Create UILabel with a 10.0 point padding around the UILabel within the parent Rect
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 10, self.bounds.origin.y + 10, self.bounds.size.width - 20, self.bounds.size.height - 20)];

textLabel.attributedText = currencyWithBalance;
textLabel.font = [UIFont fontWithName:@"TitilliumWeb-Light" size:28.0];
textLabel.minimumScaleFactor = 15.0/28.0;
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 1;
textLabel.backgroundColor = [UIColor redColor];


[self addSubview:textLabel];
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我实现这一目标,以便文本也垂直对齐吗?

多谢你们.

objective-c vertical-alignment nsattributedstring uilabel ios

5
推荐指数
1
解决办法
6454
查看次数

UITextView sizeToFit无法正常工作

奇怪的是,我一直在寻找这个答案,但似乎没有任何效果!

我很简单UITextView,我正在尝试根据其内容在viewController中调整大小:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *lorum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit …
Run Code Online (Sandbox Code Playgroud)

objective-c ios

4
推荐指数
2
解决办法
1万
查看次数

Textview Center文本对齐IOS 7

-(void)observeValueForKeyPath:(NSString *)keyPath   ofObject:(id)object   change:(NSDictionary *)change   context:(void *)context 
{
    NSLog(@"Hello");
    UITextView *tv = object;
    CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])  / 2.0;
    topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
    tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
Run Code Online (Sandbox Code Playgroud)

我在Viewdidload中这样称呼它

[myTextView1 addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios

3
推荐指数
1
解决办法
1万
查看次数

Swift - 更改TableView中标题部分的标题的垂直对齐方式

我使用以下代码填充我的函数的标题:

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    switch section {

    case 0: "SECTION 0"

    case 1: "SECTION 1"

    default: break

    }

    return nil
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    var title = UILabel()
    title.font = UIFont(name: "HelveticaNeue-Light", size: 12)!
    title.textColor = UIColor.blackColor()

    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font=title.font
    header.textLabel?.textColor=title.textColor
    header.backgroundView?.backgroundColor = UIColor.whiteColor()

}
Run Code Online (Sandbox Code Playgroud)

现在,我希望能够更改该部分中标题的垂直对齐方式,如下图所示:

更改TableView部分中标题的垂直对齐方式

我怎样才能做到这一点?

uitableview swift swift2

3
推荐指数
1
解决办法
2177
查看次数