xcode 如何使用 UITextAlignment 将 UITableViewCell 中的文本在 Swift 中左右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7162126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:47:41  来源:igfitidea点击:

How to use UITextAlignment to align text in UITableViewCell to the right and left in Swift

iphoneswiftxcodeipad

提问by Girish

I have to align text in table cell to right and left.I have used two plist in my application according to plist selection I have to align my text in table cell.

我必须将表格单元格中的文本左右对齐。根据 plist 选择,我在应用程序中使用了两个 plist 我必须将表格单元格中的文本对齐。

I try following code for that

我尝试以下代码

if ([app.currentPlist isEqualToString:@"Accounting.plist"]) {
    cell.textAlignment=UITextAlignmentLeft;            
} else {
    cell.textAlignment=UITextAlignmentRight;           
}

回答by Alex Coplan

UITableViewCelldoesn't have a textAlignmentproperty. You have to set it on the cell's text label:

UITableViewCell没有textAlignment财产。您必须将其设置在单元格的文本标签上:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = @"Something";

    if([app.currentPlist isEqualToString:@"Accounting.plist"])
    {
        cell.textLabel.textAlignment=UITextAlignmentLeft;             
    }
    else
    {
        cell.textLabel.textAlignment=UITextAlignmentRight;           
    }

    return cell;
}

回答by Rupal Chawla

UITextAlignmentRightis deprecated, use NSTextAlignmentLeftinstead

UITextAlignmentRight已弃用,请NSTextAlignmentLeft改用

So, the code will be - cell.textLabel.textAlignment = NSTextAlignmentLeft;

所以,代码将是 - cell.textLabel.textAlignment = NSTextAlignmentLeft;

回答by Bala Vishnu

In swift it is

很快就是

cell.textLabel.textAlignment = NSTextAlignment.Right