2017-08-07 99 views
1

权目前,我试图创建一个应用程序中的消息功能。调整的UILabel对齐到屏幕

我想对齐的UILabel从用户发送到屏幕右侧的消息。随着文本宽度内容的增加,我希望它可以伸展到屏幕的左侧,然后一旦达到最大宽度就换行。

正如你可以看到我有文字换行正确,但我不能让文字正确坐在屏幕上。

我对这个问题的解决办法是,以编程方式调整的UILabel的宽度,但我一直无法得到它从固定大小的我在自定义传出消息细胞的XIB文件更改

莫非有人请告知如何做到这一点?

编辑:

我画箱过的UILabel的电流范围,以显示它正在做什么 Box drawn over UILabel bounds

这是文本移动和的UILabel势必重绘解释什么,我试图做。 Box and text moved to show what I'm trying to do.

应用 Screen shot of messaging feature in application

+0

你应用了文本对齐吗? https://developer.apple.com/documentation/uikit/uilabel/1620541-textalignment – paulvs

+0

@paulvs我做了一些编辑,以更好地解释我遇到的问题。我有正确的文本对齐,是我的错误。问题是UILabel的大小我不确定如何更改 – Airagale

+0

因此,您想缩小标签以适合其内容?您是否使用约束或框架来定位标签? – paulvs

回答

0

这里的截图,你应该计算基于文本长度的标签的宽度。 尝试使用此API根据文本输入获取标签的大小。 CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:@ {NSFontAttributeName:[UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];}};

我不低于你的情况。但你可以试一试。

在Xcode中创建一个标签,使用自动布局来设置填充(大多数4点在尾随&灵活的领先空间> 4分)。请确保将行数设置为“0”并且文本的右侧对齐。不要为宽度设置固定的大小这里

+0

不幸的是,我正在寻找的解决方案是不对文本进行右对齐。 – Airagale

0

enter image description here

您可以通过测量文本的长度,并呼吁大小以适合标签上达到预期的效果。然后,将标签的框架(或需要时的周围超级视图)偏移到屏幕的左侧或右侧。

这里有一个快速的方法(可能是最好声明变量maxWidth和填充其他地方)。请注意,'w'是屏幕宽度的变量。

-(UIView *)bubbleWithText:(NSString *)text atOffset:(float)yOff isLeftAligned:(bool)leftAligned { 

    float maxWidth = 200.0f; 
    float intraCellPadding = 5.0f; 

    UILabel * label = [UILabel new]; 
    label.frame = CGRectMake(intraCellPadding, intraCellPadding, maxWidth, 0); 
    label.textColor = (leftAligned) ? [UIColor blackColor] : [UIColor whiteColor]; 
    label.text = text; 
    label.numberOfLines = 0; 
    [label sizeToFit]; 

    float width = label.frame.size.width; 
    float height = label.frame.size.height; 
    float xOff = (leftAligned) ? intraCellPadding : w-3*intraCellPadding-width; 

    UIView * bubble = [UIView new]; 
    bubble.frame = CGRectMake(xOff, yOff, width + 2 * intraCellPadding, height + 2 * intraCellPadding); 
    bubble.backgroundColor = (leftAligned) ? [UIColor greenColor] : [UIColor blueColor]; 
    bubble.layer.cornerRadius = intraCellPadding; 
    [bubble addSubview:label]; 

    return bubble; 

} 

而且你会调用上面是这样的:

float interCellPadding = 10.0f; 
float yOff = 20.0f; 
bool previousWasLeft = false; 

NSArray * texts = @[@"Bacon ipsum dolor amet kevin swine ham hock, leberkas porchetta salami bacon picanha shoulder pig strip steak sirloin.", 
        @"Short loin hamburger meatloaf jowl, sirloin swine pork belly tail t-bone venison.", 
        @"Flank meatloaf prosciutto ball tip strip steak rump.", 
        @"Short piggy.", 
        @"Tri-tip ribeye drumstick andouille leberkas bacon pork chop meatball pork spare ribs chicken.", 
        @"Strip steak filet mignon tri-tip sausage, cow frankfurter bacon ribeye cupim burgdoggen shank pork belly fatback ham hock."]; 

for (NSString * text in texts){ 

    //random assignment left/right 
    //by creating a random number, either one or zero 
    //this doubles up as a boolean 
    bool alignLeft = arc4random_uniform(2); 

    //here we increment the offset 
    //depending on whether the previous bubble 
    //was on the same side or not, giving a 
    //bit more space for alternating bubbles. 
    yOff += (previousWasLeft != alignLeft) ? interCellPadding : interCellPadding/2.0f; 
    previousWasLeft = alignLeft; 

    //create the bubble and add 
    UIView * bubble = [self bubbleWithText:text atOffset:yOff isLeftAligned:alignLeft]; 
    [self.view addSubview:bubble]; //or scrollView whatever 

    //incrmeent the offset 
    yOff += bubble.frame.size.height; 

} 

这是快速和肮脏的,但展示了如何实现你所需要的。对于很多细胞的(这可能是你想要的),如果你要自定义路线,你必须留意从内存中释放他们。调整UICollectionView单元格并简单地对齐内容可能会更容易。