ios - Position of NSTextAttachment inside UILabel? -
i have uilabel
displaying nsattributedstring
. string contains text , uiimage
nstextattachment
.
when rendered, there way position of nstextattachment
in uilabel
?
edit
here end result trying achieve.
when text 1 line long, image should right @ edge of uilabel
. simple:
the problem arises when have multiple lines, still want image @ end of last line:
i can think of 1 solution (which more workaround) , applicable in limited cases. assuming nsattributedstring
contains text on left side , image on right side, can calculate size of text , position of nstextattachment
sizewithattributes:. not complete solution, x
coordinate (i.e. width
of text part) can used.
nsstring *string = @"my text string"; uifont *font = [uifont fontwithname:@"helveticaneue-italic" size:24.0]; nsdictionary *attributes = [nsdictionary dictionarywithobjectsandkeys:font, nsfontattributename, nil]; cgsize size = [string sizewithattributes:attributes]; nslog(@"%f", size.width); // should x coordinate @ nstextattachment starts
hope provides hint.
edit:
if have lines wrapping can try following code (string
string putting in uilabel , self.testlabel
uilabel):
cgfloat totalwidth = 0; nsarray *wordarray = [string componentsseparatedbystring:@" "]; (nsstring *i in wordarray) { uifont *font = [uifont fontwithname:@"helveticaneue-italic" size:10.0]; nsdictionary *attributes = [nsdictionary dictionarywithobjectsandkeys:font, nsfontattributename, nil]; // size of string, appending space cgsize stringsize = [[i stringbyappendingstring:@" "] sizewithattributes:attributes]; totalwidth += stringsize.width; // size of space character cgsize spacesize = [@" " sizewithattributes:attributes]; // if "if" true, have line wrap if ((totalwidth - spacesize.width) > self.testlabel.frame.size.width) { // , our width size of strings on new line minus single space totalwidth = stringsize.width - spacesize.width; } } // prevents bug end of text reaches end of uilabel if (textattachment.image.size.width > self.testlabel.frame.size.width - totalwidth) { totalwidth = 0; } nslog(@"%f", totalwidth);
Comments
Post a Comment