How to use NSSuperscriptAttributeName for OS X - nsattributedstring

I can change the font and size but I'm stuck with making text a superscript.
Here is my working code for font and size:
aVerseMutableString = NSMutableAttributedString(string: book.verseText,
attributes: [NSFontAttributeName:NSFont(name: "Helvetica", size: 18.0)!])
Here is what I'm trying for superscript that's not working:
aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description,
attributes: [NSSuperscriptAttributeName:NSNumber(1)!])
I'm not sure how to do the attributes part to create a superscript.

After using NSBaselineOffsetAttributeName I figured out how to do it the way I showed above. Here are both ways:
NSSuperscriptAttributeName
aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description,
attributes: [NSSuperscriptAttributeName:NSNumber(int: 2)])
NSBaselineOffsetAttributeName
aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description,
attributes: [NSBaselineOffsetAttributeName:NSNumber(double: 2.0)])

Related

Aligning substrings in NSAttributedString

I'm writing an attributed string to a UITextView. It has a left-aligned text substring, followed by a line number. I want that line number to be right-aligned.
I tried applying an NSParagraphStyle to the citation string and then appending the citation string to the text string.
let alignStyle = NSMutableParagraphStyle()
alignStyle.alignment = .right
let citationAttributes: Dictionary<NSAttributedString.Key, Any> = [
.paragraphStyle: alignStyle]
let citationString = NSAttributedString(string: citation, attributes: citationAttributes)
textLine.append(citationString)
I expected to see the citation neatly aligned to the right margin, on the same line as the text. But instead, it occurs immediately at the end of the text string.
Is there some way to achieve the formatting result I want with an attributed string?
On another forum I was shown how to add tabs to an attributed string by using its paragraph style. That solved all my problems.
let newLine = NSMutableAttributedString(string: citation + "\t", attributes: citationAttributes)
newLine.append(NSAttributedString(string: line.text + "\n", attributes: textAttributes))
let style = NSMutableParagraphStyle()
style.lineSpacing = lineSpacingValue
let tabStop = tabStopValue
style.tabStops = [NSTextTab(textAlignment: .left, location: tabStop * 1.1, options: [:])]
newLine.addAttribute(.paragraphStyle, value: style, range: NSRange(location: 0, length: newLine.length))

Octave: Change color in drawArrow

I found this answer
[Octave : How to change line color and width in drawRect
how to change the color in drawRect.
When I try the same for drawArrow, I get:
h=drawArrow(0,0,3,1,1,1,1,1);
h =
scalar structure containing the fields:
body = -12.469
head = -11.925
set(h,'color','r');
error: octave_base_value::array_value():
wrong type argument 'scalar struct'
error: set: H must be a graphics handle
What I am doing wrong here? For me it looks the same.
Thanks
Karl
You really should mention that you are using the octave-forge geometry package. drawArrow returns a struct with handles for the arrow body and head (as shown by your code) so you can and have to set them separately:
pkg load geometry
h = drawArrow (0,0,3,1,1,1,1,1);
set (h.body, "color", "r")
set (h.head, "facecolor", get(h.body, "color"))
You can also set a border around the head with "edgecolor"

How to change the font of the label's contours in R?

I want to change the font of the labels of the contours to black italic. I have tried using "font",values 1,2,3 or 4, but it doesn't work, with any of those value I keep getting the same plot. I think I'm missing something.
This is the code:
library("ncdf4");library("maps")
file<-"http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCEP/.EMC/.CMB/.GLOBAL/.Reyn_SmithOIv2/.weekly/.sst/T/%2819-25%20Feb%202017%29VALUES/data.nc"
try(download.file(url =file ,"data.nc",quiet = F,mode="wb"))
data<-nc_open("data.nc")
latGb<-ncvar_get(data,"Y")
lonGb<-ncvar_get(data,"X")
latCb<-latGb[30:121]
lonCb<-lonGb[240:301]
dat<-ncvar_get(data,"sst")
dat<-dat[240:301,30:121]
filled.contour(lonCb, latCb, dat,zlim =c(5:35),nlevels=80,
plot.axes={
contour(lonCb, latCb, dat,nlevels=10,add=T,font=4,labcex=1);
map('world2',col="black",fill = TRUE, add=TRUE);
grid()})
This is want i keep getting:
Pic
Thanks for the help
In contour help file, vfont is a parameter you can set by providing a vector of length 2 where the first element is the typeface, and the second element is the font face. Note, font is not a parameter within contour that you can set, that is why nothing happens when you use this. Always check the help file on what parameters can be set.
I set vfont to bold italic so it appears more solid black than grey. You select the typeface you like, I used the default sans serif.
filled.contour(lonCb, latCb, dat, zlim =c(5:35), nlevels=80,
plot.axes={
contour(lonCb, latCb, dat, nlevels=10, add=T,
vfont=c("sans serif", "bold italic"), labcex=1);
map('world2', col="black", fill = TRUE, add=TRUE);
grid()})

gvisScatterChart - hAxis.title and vAxis.title not appearing on the plot

hAxis.title and vAxis.title options don't seem to set the axis titles for gvisScatterChart in the package googleVis.
plot(gvisScatterChart(data.frame(x = 3, y = 4), options=list(hAxis.title = 'foo')))
"foo" doesn't appear as the horizontal axis title.
You're supposed to pass options to hAxis in a dictionary-looking JSON object format:
plot(gvisScatterChart(data.frame(x = 3, y = 4), options=list(hAxis="{title:'foo'}")))
See the examples at the bottom of the help page and this under hAxis:
a JSON object. Default null. An object with members to configure various horizontal axis elements. To specify properties of this object, you can use object literal notation, as shown here:
{title: 'Hello', titleTextStyle: {color: '#FF0000'}}

Height required for UILabel dependent on string length (Swift, iOS 8)

I am struggling to get the height that is required for a UILabel based on variable sized text that it could accept. From hours of research, I have not yet discovered a viable way of achieving this. Currently my code is as follows:
func getHeightForTitle(postTitle: NSString) -> CGFloat {
// Get the height of the font
let constraintSize = CGSizeMake(self.cellTextWidth, CGFloat.max)
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
let labelSize = postTitle.boundingRectWithSize(constraintSize,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes,
context: nil)
return labelSize.height
}
This however throws the following error:
2014-08-02 12:09:37.370 Testing App[8365:351906] - [_TtCSs23_ContiguousArrayStorage00007FD26C15F708 pointSize]: unrecognized selector sent to instance 0x11e640050
This is always thrown at the let labelSize = postTitle... method and I believe it is down to the attributes variable. I however maybe wrong. Any help is appreciated, and much thanked!
Please note: This is for an iOS 8, Swift development project.
Two observations. First, what's wrong with your code is that this line is not Swift:
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
The stuff after the equals sign is Objective-C, and Swift is having trouble interpreting it. Remove the square brackets from around the UIFont call; you have turned this into an array, which is the source of the error you're seeing.
Second, and more important, there are many much simpler ways to do this, by letting the label tell you its size for the desired text:
Put the text into the label and call sizeToFit() on the label.
Put the text into the label and call sizeThatFits() on the label with the desired width and large height.
Under auto layout, set the label's preferredMaxLayoutWidth to the desired width and put the text into the label; it will size itself.
However, I would urge you not to do this if you don't have to. A label is already self-sizing under auto layout, and in iOS 8 there's a new feature where a table cell will self-adjust its height to its contents, so there is now very rarely a need to pre-measure a label's dimensions.
Put the text into the label and call sizeThatFits() on the label with the desired width and large height.
sub_label=UILabel(frame: CGRectMake(0, 0, self.view.bounds.width, 50))
sub_label.numberOfLines=0;
sub_label.textAlignment=NSTextAlignment.Left
sub_label.lineBreakMode=NSLineBreakMode.ByWordWrapping
let subfont = UIFont(name: "Helvetica", size: 20.0)
sub_label.font=subfont
sub_label.text="his is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of text"
sub_label.backgroundColor=UIColor.clearColor()
var textViewSizesub=sub_label.sizeThatFits(CGSizeMake(self.view.bounds.width, CGFloat.max))
sub_label.frame=CGRectMake(0, textViewSize.height, self.view.bounds.width-5, textViewSizesub.height)

Resources