归因于两个文本对齐的文本

时间:2015-10-09 03:44:56

标签: ios swift uitextview nsattributedstring

有谁知道如何在一个字符串中实现两个不同的文本对齐?

这就是我想要textView显示的内容:

fseek(list, 0, SEEK_END);
long size = ftell(list);
if (size == 0)
{
   fprintf(stdout, "list.txt is empty \n");
}

// Rewind the file
rewind(list);

cache = malloc(size+1);
if ( cache == NULL }
{
   // Unable to allocate memory.
   exit(1);
}

int n = fread(cache, 1, size, list);
if ( n != size )
{
   // Was able to read only n characters,
   // not size characters.
   // Print a message.
}

cache[n] = '\0';
fprintf(stdout, "cache is %s\n", cache);

我的代码:

label                                                                         value

我得到了什么:

let txtView = cell.viewWithTag(77) as! UITextView

let leftStyle = NSMutableParagraphStyle()
leftStyle.alignment = NSTextAlignment.Left
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = NSTextAlignment.Right

let attText = NSMutableAttributedString(string: "label", attributes: [NSParagraphStyleAttributeName: leftStyle])
attText.appendAttributedString(NSAttributedString(string: " "))
attText.appendAttributedString(NSAttributedString(string: "value", attributes: [NSParagraphStyleAttributeName: rightStyle]))

txtView.attributedText = attText

任何帮助表示赞赏! :)

2 个答案:

答案 0 :(得分:13)

NSMutableParagraphStyleNSTextTab一起使用:

let paragraph = NSMutableParagraphStyle()
paragraph.tabStops = [
    NSTextTab(textAlignment: .Right, location: 100, options: [:]),
]

let str = "Label\tValue\n"
    + "foo\tbar\n"

let attributed = NSAttributedString(
    string: str,
    attributes: [NSParagraphStyleAttributeName: paragraph]
)

let view = UITextView(frame: CGRectMake(0, 0, 120, 120))
view.textContainer.lineFragmentPadding = 10

view.attributedText = attributed

screenshot

当然,这与“tabstop”对齐,但不与UITextView的边缘对齐。修改视图大小时,还必须修改location的{​​{1}}。

答案 1 :(得分:-1)

df1 <- data.frame(Firstname = c("Eve", "Jon", "Steve"), 
                  Lastname = c("Jackson", "Smith", "Jackson"),
                  Email = c("evejackson@yahoo.co.uk", "johnsmith@gmail.com",
                            "stevejackson@yahoo.com"),
                  stringsAsFactors = FALSE)
# df1
      Firstname Lastname                  Email
1       Eve  Jackson evejackson@yahoo.co.uk
2       Jon    Smith    johnsmith@gmail.com
3     Steve  Jackson stevejackson@yahoo.com

df2 <- data.frame(Firstname = c("Jon", "Samantha", "Steve"), 
                  Lastname = c('Smith', "Andrew", "Jackson"),
                  Email = c("johnsmith@gmail.com", "samanthaandrew@yahoo.co.uk",
                            "stevejackson@yahoo.co.uk"),
                  stringsAsFactors = FALSE)
# df2
  Firstname Lastname                      Email
1       Jon    Smith        johnsmith@gmail.com
2  Samantha   Andrew samanthaandrew@yahoo.co.uk
3     Steve  Jackson   stevejackson@yahoo.co.uk

# check if Emails in df1 are also in df2 and then if Firstname and Lastname are the
# same in df1 and df2 
df1$Status <- ifelse(df1$Email %in% df2$Email, "Registered",
                     ifelse(df1$Firstname == df2$Firstname &
                                    df1$Lastname == df2$Lastname, "Registered",
                            ""))
df1 # output
  Firstname Lastname                  Email     Status
1       Eve  Jackson evejackson@yahoo.co.uk           
2       Jon    Smith    johnsmith@gmail.com Registered
3     Steve  Jackson stevejackson@yahoo.com Registered
相关问题