objective c - iOS: The proper way to display Images and long texts with scroll functionality -
i'm developing app news website, i'm displaying news articles using uitableview each cell article title, when user clicks on cell (i.e article), view opens (using segue), in view want put following:
- the article's image @ top.
- the article's date under image.
- the article's description under date. (which long)
- the ability user scroll entire view. (not description)
note: have tried many ways, can't seem know proper way implement structure.
the modern solution relatively simple: compose whole thing attributed string , put uitextview. text view automatically deal fact description may long, content should scroll together, etc.
e.g.
nsattributedstring *imagestring = [nsattributedstring attributedstringwithattachment: [[nstextattachment new] setimage:[uiimage imagenamed:@"whatever.png"]]]; ... , use natural means composition of attributed strings , setting things font , colour on other bits of text. textview.attributedstring = compoundstring;.
elaborated example:
- (void)setstory:(story *)story { nsattributedstring *image = [story imagestring]; nsattributedstring *date = [story datestring]; nsattributedstring *body = [story bodystring]; nsmutableattributedstring *wholestory = [nsmutableattributedstring new]; // todo: can sure image, date , body non-nil? nsarray *allcomponents = @[image, date, body]; for(nsattributedstring *component in allcomponents) { [wholestory appendattributedstring:component]; if(component != [allcomponents lastobject]) [[wholestory mutablestring] appendstring:@"\n\n"]; } self.textview.attributedstring = wholestory; } ... elsewhere, in story object ... - (uiimage *)image { // ...something... } - (nsstring *)datetext { // ...something, using nsdateformatter unless it's returned // server or wherever formatted... } - (nsstring *)bodytext { // ... ... } - (nsattributedstring *)imagestring { return [nsattributedstring attributedstringwithattachment: [[nstextattachment new] setimage:[self image]]]; } - (nsattributedstring *)datestring { return [[nsattributedstring alloc] initwithstring:[self datetext] attributes: @{ nsfontattributename: [uifont preferredfontfortextstyle: uifonttextstylesubheadline], ... etc ... }]; } - (nsattributedstring *)bodystring { return [[nsattributedstring alloc] initwithstring:[self bodytext] attributes: @{ nsfontattributename: [uifont preferredfontfortextstyle: uifonttextstylebody], ... etc ... }]; } check out nsattributedstring uikit additions documentation lists of various attributes can set other nsfontattributename. note i've gone ios 7+ way of asking fonts purpose rather specific size or font. means users have turned default font size larger text in app.
Comments
Post a Comment